// const BASE_URL = "http://112.74.164.157:7001/"
const BASE_URL = "http://127.0.0.1:7001/"

uni.$u.http.setConfig(config => {
  config.baseURL = BASE_URL
  config.timeout = 8000
  config.header = {
    'content-type': 'application/x-www-form-urlencoded' || 'application/json',
    // 'token': uni.getStorageSync('token')
  }
  return config
})
uni.$u.http.interceptors.request.use(config => {
  config.data = config.data || {}
  config.header.token = `Bearer ${uni.getStorageSync('token')}`
  return config
}, config => {
  // return Promise.reject(config)
})

uni.$u.http.interceptors.response.use(res => {
  // console.log(res)
  if (res.data.code === 500) {
    uni.$u.toast('服务器错误')
  }
  return res.data
}, res => {
  if (res.data.code == 401) {
    return uni.showLoading({
      title: '登录失效',
      success: () => {
        setTimeout(() => {
          uni.hideLoading();
          uni.switchTab({
            url: '/pages/profile/index'
          })
        }, 2000)
      }
    })
  } else {
    uni.$u.toast('请求超时！')
  }
})

export default uni.$u.http
